package com.njust.controller;

import com.njust.Util.ExcelUtil;
import com.njust.domain.dto.ProjectCreateDTO;
import com.njust.domain.dto.ProjectDTO;
import com.njust.domain.dto.ProjectPageQueryDTO;
import com.njust.domain.po.Project;
import com.njust.domain.vo.ProjectPageQueryVO;
import com.njust.domain.vo.ProjectVO;
import com.njust.exception.ExcelExistsRepeatedProject;
import com.njust.exception.ExcelFileIsNull;
import com.njust.mapper.ProjectMapper;
import com.njust.result.Result;
import com.njust.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/7 13:03
 * @项目controller
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/project")
@Slf4j
public class ProjectController {
    @Autowired
    private ProjectService service;

    /**
     * 添加项目
     * @param projectCreateDTO
     * @return
     */
    @PostMapping("/add")
    public Result<String> create(@RequestBody ProjectCreateDTO projectCreateDTO) {
        log.info("创建项目:{}", projectCreateDTO);
        service.add(projectCreateDTO);
        return Result.success("创建项目成功");
    }

    @PostMapping("/import")
    public Result<String> importFromExcel(@RequestParam MultipartFile file) throws IOException {
        log.info("导入项目:{}", file);
        if(file==null||file.isEmpty()) {
            throw new ExcelFileIsNull("导入的Excel文件为空，请重新上传有效的Excel文件");
        }
        InputStream inputStream = file.getInputStream();
        try(inputStream){
            List<ProjectCreateDTO> data = ExcelUtil.readExcel(ProjectCreateDTO.class,inputStream, "增加内容提醒清单");
            HashSet<String> set = new HashSet<>();
            boolean isRepeated = data.stream().anyMatch(i -> !set.add(i.getProduct_name()+ i.getProject_name()));
            if(isRepeated) {
                throw new ExcelExistsRepeatedProject("Excel表里面存在重复的项目");
            }
            service.importExcel(data, set);
        }catch (IOException e){
            e.printStackTrace();
        }
        return Result.success("导入项目成功");
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody ProjectDTO projectDTO) {
        log.info("修改项目:{}", projectDTO);
        service.update(projectDTO);
        return Result.success("修改项目成功");
    }

    @GetMapping("/pageQuery")
    public Result<ProjectPageQueryVO> pageQuery(ProjectPageQueryDTO projectPageQueryDTO) {
        log.info("分页查询:{}", projectPageQueryDTO);
        ProjectPageQueryVO projectPageQueryVO = service.pageQuery(projectPageQueryDTO);
        return Result.success(projectPageQueryVO, "分页查询成功");
    }
    @GetMapping("/{project_id}")
    public Result<ProjectVO> selectById(@PathVariable int project_id) {
        log.info("根据项目id查询:{}", project_id);
        ProjectVO projectVO = service.selectById(project_id);
        return Result.success(projectVO, "根据id查找成功");
    }
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam int project_id) {
        log.info("根据id删除项目:{}", project_id);
        service.delete(project_id);
        return Result.success("删除成功");
    }
}
